package src.main.java.com.pms.gui.dialog.appointment;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.ResultSet;
import src.main.java.com.pms.service.AppointmentService;

public class AppointmentListDialog extends JDialog {
    private JTable appointmentTable;
    private DefaultTableModel tableModel;
    private AppointmentService appointmentService;

    public AppointmentListDialog(JFrame parent) {
        super(parent, "预约列表", true);
        appointmentService = new AppointmentService();
        initComponents();
        loadAppointmentData();
    }

    private void initComponents() {
        setSize(600, 400);
        setLocationRelativeTo(getParent());

        // 创建表格模型
        String[] columnNames = {"ID", "宠物名称", "洗澡预约", "疫苗预约", "火葬预约"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        appointmentTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(appointmentTable);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton refreshButton = new JButton("刷新");
        JButton deleteButton = new JButton("删除");
        JButton editButton = new JButton("编辑");

        refreshButton.addActionListener(e -> loadAppointmentData());
        deleteButton.addActionListener(e -> deleteAppointment());
        editButton.addActionListener(e -> editAppointment());

        buttonPanel.add(refreshButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(editButton);

        // 设置布局
        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }



    private void loadAppointmentData() {
        try {
            // 清空表格
            tableModel.setRowCount(0);

            // 获取预约数据
            ResultSet rs = appointmentService.getAllAppointments();
            while (rs.next()) {
                Object[] row = {
                        rs.getInt("id"),
                        rs.getString("name"),
                        convertBoolean(rs.getInt("has_bath_appointment")),
                        convertBoolean(rs.getInt("has_vaccine_appointment")),
                        convertBoolean(rs.getInt("has_cremation_appointment"))
                };
                tableModel.addRow(row);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "加载数据失败：" + e.getMessage(),
                    "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void deleteAppointment() {
        int selectedRow = appointmentTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要取消的预约！",
                    "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int id = (Integer) tableModel.getValueAt(selectedRow, 0);
        String appointmentType = "";

        if ("是".equals(tableModel.getValueAt(selectedRow, 2))) {
            appointmentType = "bath";
        } else if ("是".equals(tableModel.getValueAt(selectedRow, 3))) {
            appointmentType = "vaccine";
        } else if ("是".equals(tableModel.getValueAt(selectedRow, 4))) {
            appointmentType = "cremation";
        } else {
            JOptionPane.showMessageDialog(this, "该宠物没有预约！",
                    "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int confirm = JOptionPane.showConfirmDialog(this,
                "确定要取消该预约吗？", "确认取消",
                JOptionPane.YES_NO_OPTION);

        if (confirm == JOptionPane.YES_OPTION) {
            try {
                appointmentService.cancelAppointment(id, appointmentType);
                loadAppointmentData();
                JOptionPane.showMessageDialog(this, "预约取消成功！");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "取消预约失败：" + e.getMessage(),
                        "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    private void editAppointment() {
        int selectedRow = appointmentTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要编辑的预约！",
                    "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int id = (Integer) tableModel.getValueAt(selectedRow, 0);
        boolean hasBathAppointment = "是".equals(tableModel.getValueAt(selectedRow, 2));
        boolean hasVaccineAppointment = "是".equals(tableModel.getValueAt(selectedRow, 3));
        boolean hasCremationAppointment = "是".equals(tableModel.getValueAt(selectedRow, 4));

        new EditAppointmentDialog(this, id, hasBathAppointment, hasVaccineAppointment, hasCremationAppointment).setVisible(true);
        loadAppointmentData();
    }

    private String convertBoolean(int value) {
        return value == 1 ? "是" : "否";
    }
}
